// 经纪人评价页脚本
document.addEventListener('DOMContentLoaded', function() {
    // 获取URL中的经纪人ID和名称
    const urlParams = new URLSearchParams(window.location.search);
    const agentId = urlParams.get('id');
    const agentName = urlParams.get('name');
    
    // 如果没有agentId，显示错误信息
    if (!agentId) {
        showError('未找到经纪人信息');
        return;
    }
    
    // 加载经纪人评价
    loadAgentReviews(agentId, agentName);
});

// 加载经纪人评价
function loadAgentReviews(agentId, agentName) {
    // 在真实应用中，这里应该从服务器获取数据
    // 这里我们使用模拟数据
    const agentData = getAgentData(agentId);
    
    // 如果找不到对应的经纪人数据，显示错误
    if (!agentData) {
        showError('未找到该经纪人信息');
        return;
    }
    
    // 合并初始评价和更多评价数据
    const allReviews = [...agentData.reviews, ...getExtraReviews(agentId)];
    
    // 渲染评价页面
    renderReviewsPage(agentData, allReviews);
    
    // 初始化筛选按钮
    initFilterButtons(allReviews);
}

// 获取经纪人数据（模拟数据）
function getAgentData(agentId) {
    // 模拟的经纪人数据
    const agentsData = {
        'agent1': {
            id: 'agent1',
            name: '张明',
            title: '前BAT高级HR',
            avatar: 'https://images.pexels.com/photos/3760263/pexels-photo-3760263.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
            rating: 4.9,
            reviewCount: 243,
            level: '金牌',
            reviews: [
                {
                    name: '李同学',
                    avatar: 'https://images.pexels.com/photos/1222271/pexels-photo-1222271.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                    rating: 5,
                    date: '2023-03-15',
                    content: '张老师的简历指导非常专业，帮我重新梳理了项目经历，突出了核心能力。修改后的简历投递通过率提高了50%以上，最终收到了字节跳动的offer！'
                },
                {
                    name: '王同学',
                    avatar: 'https://images.pexels.com/photos/733872/pexels-photo-733872.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                    rating: 5,
                    date: '2023-02-20',
                    content: '面试模拟非常有帮助，张老师提前帮我准备了可能遇到的各种问题，还针对我的回答给了很多中肯的建议。最终顺利通过了阿里的三轮面试，感谢张老师！'
                },
                {
                    name: '陈同学',
                    avatar: 'https://images.pexels.com/photos/1681010/pexels-photo-1681010.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                    rating: 4,
                    date: '2023-01-10',
                    content: '全程跟踪服务非常贴心，从简历优化到面试准备再到offer选择，张老师都给了很多专业建议。唯一的小遗憾是有时回复不够及时，但总体来说非常满意。'
                }
            ]
        },
        'agent2': {
            id: 'agent2',
            name: '王静',
            title: '资深金融顾问',
            avatar: 'https://images.pexels.com/photos/5393594/pexels-photo-5393594.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
            rating: 5.0,
            reviewCount: 156,
            level: '金牌',
            reviews: [
                {
                    name: '刘同学',
                    avatar: 'https://images.pexels.com/photos/1043471/pexels-photo-1043471.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                    rating: 5,
                    date: '2023-03-20',
                    content: '王老师的金融行业分析非常深入，帮我理清了不同细分领域的特点和要求，最终帮助我找到了最适合自己的方向。在她的指导下，我顺利拿到了高盛的offer！'
                },
                {
                    name: '张同学',
                    avatar: 'https://images.pexels.com/photos/937481/pexels-photo-937481.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                    rating: 5,
                    date: '2023-02-15',
                    content: '案例分析课非常有价值，王老师分享了很多真实的投行案例和解题思路，让我在后续面试中表现得非常出色。强烈推荐给想进入金融行业的同学！'
                }
            ]
        },
        'agent3': {
            id: 'agent3',
            name: '李华',
            title: '金融行业导师',
            avatar: 'https://images.pexels.com/photos/3785104/pexels-photo-3785104.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
            rating: 4.8,
            reviewCount: 120,
            level: '银牌',
            reviews: [
                {
                    name: '赵同学',
                    avatar: 'https://images.pexels.com/photos/2379005/pexels-photo-2379005.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                    rating: 5,
                    date: '2023-03-10',
                    content: '李老师的职业规划课程让我对金融行业有了更清晰的认识，找到了适合自己的发展方向。简历修改也非常专业，投递成功率大大提高。'
                },
                {
                    name: '孙同学',
                    avatar: 'https://images.pexels.com/photos/1516680/pexels-photo-1516680.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                    rating: 4,
                    date: '2023-01-25',
                    content: '求职辅导很有针对性，李老师分享了很多行业内部的见解和面试技巧。唯一的建议是希望能增加更多实践性的内容，但总体来说非常满意。'
                }
            ]
        }
    };
    
    return agentsData[agentId];
}

// 获取更多评价数据（模拟数据）
function getExtraReviews(agentId) {
    // 额外的评价数据
    const extraReviews = {
        'agent1': [
            {
                name: '郑同学',
                avatar: 'https://images.pexels.com/photos/1438072/pexels-photo-1438072.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 5,
                date: '2022-12-20',
                content: '张老师的简历指导非常专业，帮我发现了很多简历中的亮点和不足，修改后投递成功率提高了很多！'
            },
            {
                name: '黄同学',
                avatar: 'https://images.pexels.com/photos/2531553/pexels-photo-2531553.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 4,
                date: '2022-11-15',
                content: '面试辅导课非常有帮助，张老师给了我很多针对性的建议，最终成功通过了几家大厂的面试。'
            },
            {
                name: '马同学',
                avatar: 'https://images.pexels.com/photos/91227/pexels-photo-91227.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 5,
                date: '2022-10-08',
                content: '全程跟踪服务非常贴心，从简历修改到面试准备再到offer选择，每一步都得到了专业指导，最后拿到了满意的offer！'
            },
            {
                name: '林同学',
                avatar: 'https://images.pexels.com/photos/874158/pexels-photo-874158.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 4,
                date: '2022-09-12',
                content: '张老师的面试技巧指导非常实用，针对不同公司的面试风格给了不同的准备建议，收获很大。'
            },
            {
                name: '吴同学',
                avatar: 'https://images.pexels.com/photos/6626903/pexels-photo-6626903.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 5,
                date: '2022-08-25',
                content: '简历修改服务非常专业，张老师对我的教育背景和项目经历进行了重新组织，简历变得更有竞争力了。'
            },
            {
                name: '赵同学',
                avatar: 'https://images.pexels.com/photos/2379005/pexels-photo-2379005.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 3,
                date: '2022-07-30',
                content: '整体服务还不错，但希望能有更多针对性的建议，特别是对非互联网行业的求职指导。'
            }
        ],
        'agent2': [
            {
                name: '钱同学',
                avatar: 'https://images.pexels.com/photos/774909/pexels-photo-774909.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 5,
                date: '2022-12-05',
                content: '王老师的金融行业分析非常深入，帮我理清了不同细分领域的特点和要求，最终找到了最适合自己的方向。'
            },
            {
                name: '孙同学',
                avatar: 'https://images.pexels.com/photos/220453/pexels-photo-220453.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 4,
                date: '2022-11-10',
                content: '案例分析课程给了我很多实用的方法和思路，对后续面试有很大帮助，推荐给想进入金融行业的同学！'
            },
            {
                name: '周同学',
                avatar: 'https://images.pexels.com/photos/1520760/pexels-photo-1520760.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 5,
                date: '2022-10-18',
                content: '王老师的投行面试辅导非常专业，从简历准备到案例分析再到行为面试，全方位提升了我的竞争力。'
            },
            {
                name: '吴同学',
                avatar: 'https://images.pexels.com/photos/1239291/pexels-photo-1239291.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 5,
                date: '2022-09-20',
                content: '求职咨询课程内容丰富，对金融行业的入职指导非常详细，帮助我快速找到了适合自己的发展方向。'
            }
        ],
        'agent3': [
            {
                name: '赵同学',
                avatar: 'https://images.pexels.com/photos/2379005/pexels-photo-2379005.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 5,
                date: '2023-03-10',
                content: '李老师的职业规划课程让我对金融行业有了更清晰的认识，找到了适合自己的发展方向。简历修改也非常专业，投递成功率大大提高。'
            },
            {
                name: '孙同学',
                avatar: 'https://images.pexels.com/photos/1516680/pexels-photo-1516680.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 4,
                date: '2023-01-25',
                content: '求职辅导很有针对性，李老师分享了很多行业内部的见解和面试技巧。唯一的建议是希望能增加更多实践性的内容，但总体来说非常满意。'
            },
            {
                name: '徐同学',
                avatar: 'https://images.pexels.com/photos/614810/pexels-photo-614810.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 5,
                date: '2022-12-12',
                content: '简历优化服务非常专业，李老师帮我重新组织了项目经验和技能展示，面试邀约数量明显增加，非常感谢！'
            },
            {
                name: '钱同学',
                avatar: 'https://images.pexels.com/photos/3778603/pexels-photo-3778603.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 4,
                date: '2022-11-20',
                content: '李老师提供的金融行业求职指导非常有价值，从简历到面试再到offer选择，都给了很多实用的建议。'
            },
            {
                name: '陈同学',
                avatar: 'https://images.pexels.com/photos/1036623/pexels-photo-1036623.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
                rating: 3,
                date: '2022-10-15',
                content: '整体服务不错，但希望能有更多针对性的反馈，特别是在面试模拟环节，可以多一些实战演练。'
            }
        ]
    };
    
    return extraReviews[agentId] || [];
}

// 渲染评价页面
function renderReviewsPage(agentData, allReviews) {
    const contentContainer = document.getElementById('reviews-content');
    
    // 清空加载指示器
    contentContainer.innerHTML = '';
    
    // 计算评分分布
    const ratingDistribution = calculateRatingDistribution(allReviews);
    
    // 创建页面内容
    let content = `
        <!-- 经纪人信息 -->
        <div class="agent-info">
            <img src="${agentData.avatar}" alt="${agentData.name}" class="agent-avatar">
            <div>
                <h2 class="agent-name">${agentData.name}</h2>
                <p class="agent-title">${agentData.title}</p>
            </div>
        </div>

        <!-- 评分汇总 -->
        <div class="rating-summary">
            <div class="rating-overall">
                <div class="rating-number">${agentData.rating}</div>
                <div class="rating-stars">
                    ${Array(Math.floor(agentData.rating)).fill('<i class="bi bi-star-fill"></i>').join('')}
                    ${agentData.rating % 1 > 0 ? '<i class="bi bi-star-half"></i>' : ''}
                    ${Array(5 - Math.ceil(agentData.rating)).fill('<i class="bi bi-star"></i>').join('')}
                </div>
                <div class="rating-count">${agentData.reviewCount}条评价</div>
            </div>
            <div class="rating-distribution">
                ${[5, 4, 3, 2, 1].map(star => `
                    <div class="rating-bar">
                        <div class="rating-level">${star}星</div>
                        <div class="bar-container">
                            <div class="bar-fill" style="width: ${ratingDistribution[star]}%;"></div>
                        </div>
                        <div class="bar-percent">${ratingDistribution[star]}%</div>
                    </div>
                `).join('')}
            </div>
        </div>

        <!-- 评价筛选 -->
        <div class="review-filter">
            <button class="filter-btn active" data-filter="all">全部</button>
            <button class="filter-btn" data-filter="5">5星 (${countRatings(allReviews, 5)})</button>
            <button class="filter-btn" data-filter="4">4星 (${countRatings(allReviews, 4)})</button>
            <button class="filter-btn" data-filter="3">3星 (${countRatings(allReviews, 3)})</button>
            <button class="filter-btn" data-filter="1-2">1-2星 (${countRatings(allReviews, 1) + countRatings(allReviews, 2)})</button>
        </div>

        <!-- 评价列表 -->
        <div class="review-list" id="review-list">
            ${allReviews.map(review => `
                <div class="review-card fade-in" data-rating="${review.rating}">
                    <div class="reviewer">
                        <img src="${review.avatar}" alt="${review.name}" class="reviewer-avatar">
                        <div class="reviewer-info">
                            <h3 class="reviewer-name">${review.name}</h3>
                            <p class="review-date">${review.date}</p>
                        </div>
                        <div class="reviewer-rating">
                            ${Array(review.rating).fill('<i class="bi bi-star-fill"></i>').join('')}
                            ${Array(5 - review.rating).fill('<i class="bi bi-star"></i>').join('')}
                        </div>
                    </div>
                    <p class="review-text">${review.content}</p>
                </div>
            `).join('')}
        </div>
    `;
    
    // 添加内容到页面
    contentContainer.innerHTML = content;
}

// 计算评分分布
function calculateRatingDistribution(reviews) {
    const distribution = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0};
    const totalReviews = reviews.length;
    
    // 统计各评分的数量
    reviews.forEach(review => {
        distribution[review.rating]++;
    });
    
    // 计算百分比
    Object.keys(distribution).forEach(rating => {
        distribution[rating] = Math.round((distribution[rating] / totalReviews) * 100);
    });
    
    return distribution;
}

// 统计指定评分的数量
function countRatings(reviews, rating) {
    return reviews.filter(review => review.rating === rating).length;
}

// 初始化筛选按钮
function initFilterButtons(reviews) {
    const filterButtons = document.querySelectorAll('.filter-btn');
    const reviewCards = document.querySelectorAll('.review-card');
    
    filterButtons.forEach(button => {
        button.addEventListener('click', function() {
            // 更新按钮状态
            filterButtons.forEach(btn => btn.classList.remove('active'));
            this.classList.add('active');
            
            // 获取筛选条件
            const filter = this.getAttribute('data-filter');
            
            // 筛选评价
            reviewCards.forEach(card => {
                const rating = parseInt(card.getAttribute('data-rating'));
                
                if (filter === 'all') {
                    card.style.display = '';
                } else if (filter === '1-2') {
                    card.style.display = (rating <= 2) ? '' : 'none';
                } else {
                    card.style.display = (rating === parseInt(filter)) ? '' : 'none';
                }
            });
            
            // 检查是否有可见的评价
            const visibleCards = document.querySelectorAll('.review-card[style="display: none;"]');
            if (visibleCards.length === reviewCards.length) {
                showNoReviews(filter);
            } else {
                const noReviews = document.querySelector('.no-reviews');
                if (noReviews) {
                    noReviews.remove();
                }
            }
        });
    });
}

// 显示没有评价的提示
function showNoReviews(filter) {
    const reviewList = document.getElementById('review-list');
    const noReviews = document.createElement('div');
    noReviews.className = 'no-reviews';
    
    let message = '暂无评价';
    if (filter !== 'all') {
        message = `暂无${filter}星评价`;
    }
    
    noReviews.innerHTML = `
        <i class="bi bi-chat-square-text"></i>
        <p>${message}</p>
    `;
    
    // 删除已有的提示
    const existingNoReviews = document.querySelector('.no-reviews');
    if (existingNoReviews) {
        existingNoReviews.remove();
    }
    
    reviewList.appendChild(noReviews);
}

// 显示错误信息
function showError(message) {
    const contentContainer = document.getElementById('reviews-content');
    contentContainer.innerHTML = `
        <div class="error-container" style="text-align: center; padding: 40px 20px;">
            <i class="bi bi-exclamation-triangle" style="font-size: 40px; color: #ff6b6b; margin-bottom: 20px;"></i>
            <p style="font-size: 16px; color: #666;">${message}</p>
            <button onclick="window.history.back()" style="margin-top: 20px; padding: 8px 20px; background-color: var(--primary-color); color: white; border: none; border-radius: 20px; cursor: pointer;">返回上一页</button>
        </div>
    `;
} 